const Order = require('../model/order');

module.exports = {

  /**
   * 添加订单的中间件
   * @param request
   * @param response
   * @param next
   */
  sOrder: (request, response, next) => {
    let {order} = request.body

    console.log('MID--------order_code = ' + order.order_code + '; user_username = ' + order.user_username +
      '; time = ' + order.time + '; amount = ' + order.amount + ';-------------------');
    Order.setOrder(order)
      .then(result => {
        request.add_oid = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 获取指定订单的条数
   * @param request
   * @param response
   * @param next
   */
  gOrderCount: (request, response, next) => {
    let {username, code, lessAmount, moreAmount, lessDate, moreDate} = request.body

    Order.getOrderCount(username, code, lessAmount, moreAmount, lessDate, moreDate)
      .then(result => {
        request.orderCount = result
        next()
      })
      .catch(error => {
        next(error)
      })

  },

  /**
   * 获取指定的订单信息的中间件
   * @param request
   * @param response
   * @param next
   */
  gOrder: (request, response, next) => {
    let start = response.start
    let size = response.size
    let {username, code, lessAmount, moreAmount, lessDate, moreDate} = request.body

    Order.getOrder(start, size, username, code, lessAmount, moreAmount, lessDate, moreDate)
      .then(result => {
        request.recordCountOrder = result
        next()
      })
      .catch(error => {
        next(error)
      })
  }
}
